using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MathematicallySafe.HeadCounter.Services.Exporters
{
    public delegate XElement ContentGenerator();

    public class PageController: IEnumerable<Page>
    {
        private readonly Dictionary<string, Page> _pages = new Dictionary<string, Page>();

        public void Add(string filename, string pagename, ContentGenerator generator)
        {
            _pages.Add(filename, new Page
                                 {
                                     Name = pagename,
                                     Filename = filename,
                                     Generator = generator
                                 });
        }

        public Page this[string filename]
        {
            get
            {
                Page page;
                _pages.TryGetValue(filename, out page);

                return page;
            }
        }

        public IEnumerator<Page> GetEnumerator()
        {
            return _pages.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public XElement GenerateTabSection(string selected)
        {
            var tabs = new XElement("ul", new XAttribute("id", "tabmenu"),
                                    from t in _pages
                                    select new XElement("li", t.Key == selected
                                                            ? new XElement("a", new XAttribute("class", "selected"), t.Value.Name)
                                                            : new XElement("a", new XAttribute("href", t.Key), t.Value.Name)
                                        )
                );

            return tabs;
        }

    }
}